package com.example.demo.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.RoleDao;
import com.example.demo.dao.WorkerDao;
import com.example.demo.pojo.Message;
import com.example.demo.pojo.Worker;
import com.example.demo.pojo.Worktype;
import com.example.demo.service.GetRoleService;
import com.example.demo.utl.IdcardValidator;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class GetRoleServiceImpl implements GetRoleService {
    @Resource
    RoleDao roleDao;
    @Resource
    IdcardValidator idcardValidator;
    @Resource
    WorkerDao workerDao;
    @Override
    public String getRole() {
      List<Worktype> worktypes = roleDao.GetRole();
      String js = JSONObject.toJSONString(worktypes);
        return js;
    }

    @Override
    public String workRegist(HttpSession session, String workname, String workphone, String workage, String worknumber, String workpass, String workpass2, String roleoption, String workquestionoption, String workanswer) {

        //去除空格
        workname= workname.replaceAll(" ","");
        workphone= workphone.replaceAll(" ","");
        workage= workage.replaceAll(" ","");
        worknumber= worknumber.replaceAll(" ","");
        workpass= workpass.replaceAll(" ","");
        workpass2= workpass2.replaceAll(" ","");
        roleoption= roleoption.replaceAll(" ","");
        workquestionoption= workquestionoption.replaceAll(" ","");
        workanswer= workanswer.replaceAll(" ","");

        Message message =new Message();
        //判断合法性
        //判断非空
        if(workname!="" && workphone!="" && workage!="" && worknumber!="" && workpass!="" && workpass2!="" && workanswer!=""){
            //判断身份证
           if(idcardValidator.isValidatedAllIdcard(worknumber)){
               //判断手机
                if(idcardValidator.isMobileNumber(workphone)){
                    if(idcardValidator.isNumeric(workage)) {
                        //判断年龄
                    int age =Integer.parseInt(workage);
                        if (age >= 18 && age <= 60) {
                            //判断验证密码一致
                            if (workpass.equals(workpass2)) {
                                //判断密码合法性
                                if (idcardValidator.isPass(workpass)) {
                                    //验证身份证是否重复
                                    if(getWorknumber(worknumber)){

                                        Worker worker = new Worker();

                                        worker.setGrName(workname);
                                        worker.setGrPhone(workphone);

                                        worker.setGrAge(age);

                                        worker.setGrNumber(worknumber);
                                        worker.setGrPwd(workpass);
                                        int role = Integer.parseInt(roleoption);

                                        worker.setJobId(role);
                                        worker.setGrQuestion(workquestionoption);
                                        worker.setGrAnswer(workanswer);

                                        //插入数据
                                        int re = workerDao.workRegeist(worker);
                                        int seq = worker.getGrSeq();

                                        String str = seq + "";
                                        String account = "GR";
                                        for (int i = 0; i < 9 - str.length(); i++) {
                                            account += "0";
                                        }
                                        account += str;
                                        message.setAccount(account);
                                        message.setInf("注册成功");

                                        session.setAttribute("account",account);

                                    }else {
                                        message.setInf("该身份已注册");
                                    }
                                } else {
                                    message.setInf("密码不合法，密码长度 6-16 位，且必须包括字母、数字、特殊符号中的3种");
                                }
                            } else {
                                message.setInf(" 密码不一致");
                            }
                        } else {
                            message.setInf("年龄不合法，应在18岁到60岁");
                        }
                    }else{
                        message.setInf("年龄不合法，请输入数字");
                    }
                }else{
                    message.setInf("手机号码不正确");
                }
           }else{
               message.setInf("身份证号不正确");
           }
        }else{
            message.setInf("数据不能为空");
        }
        return JSONObject.toJSONString(message);
    }

    //验证身份证
    boolean getWorknumber(String worknumber){
       Worker worker= workerDao.getWorknumber(worknumber);
       if(worker!=null){
           return false;
       }else {
           return true;
       }
    }
}
